﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;


namespace SPBusiness
{
   public sealed class Util
    {
       private const string HTML_LYNC = "<nobr><span><a onclick=\"GoToLink(this);return false;\" href=\"/_layouts/userdisp.aspx?ID={0}\">{1}</a><img width=\"3\" height=\"1\" src=\"/_layouts/images/blank.gif\" border=\"0\"/><a class=\"ms-imnlink\" onclick=\"IMNImageOnClick(event);return false;\" href=\"javascript:;\"><img id=\"customimn{3}\" src=\"/_layouts/images/imnhdr.gif\" style=\"border-width:0px\" alt=\"OCS\" SIP=\"{2}\"></a></span></nobr>";
       private const string LYNC_LOAD_JS = @"<script type='text/javascript'>$(document).ready(function(){$('img[id^=""customimn""]').each(function () {setTimeout(""IMNRC('"" + $(this).attr(""SIP"") + ""', document.getElementById('"" + $(this).attr(""id"") + ""'));"", 100);});});</script>";
   
              /// <summary>
       /// Util class has only Static Methods, no constructor needed.
       /// </summary>
       private Util() { }
        public static string GetCurrentDomain()
        {
            string domain = string.Empty;

            try
            {
                domain = System.Environment.UserDomainName;
                return domain;
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Util).Namespace, typeof(Util).Name, "GetCurrentDomain", ex);
            }            

            try
            {
             domain = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain().GetDirectoryEntry().Properties["name"].Value.ToString();
             return domain;
            }
            catch (Exception ex)
            { 
                Logging.LogMessase(typeof(Util).Namespace, typeof(Util).Name, "GetCurrentDomain", ex);
                throw;
            }
        }
        public static void DeleteSPFile(string pURL, SPWeb pSPWeb)
        {
            try
            {

                if (pSPWeb != null)
                {
                    SPFile vSPFile = pSPWeb.GetFile(pURL);

                    if ((vSPFile != null) && (vSPFile.Exists))
                    {
                        vSPFile.Delete();
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Util).Namespace, typeof(Util).Name, "DeleteSPFile", ex);
                throw;
            }
        }

        public static string getUserPresense(SPUser user)
        {
           return getUserPresense(user.ID, user.Name, user.Email);
        }

        public static string getUserPresense(int userid, string username, string sipaddress)
        {
           Random rd = new Random();
           return string.Format(HTML_LYNC,
               userid, username, sipaddress, rd.Next(1, 1000000));
        }
        public static string getUserPresenseLoadScript()
        {
           return LYNC_LOAD_JS;
        }

    }
}
